package com.se.Login;

import com.se.utils.ServiceUtils;
import com.se.utils.StageManager;
import com.se.utils.StageType;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.awt.*;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

public class LoginApplication extends Application {

    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(LoginApplication.class
                .getResource("/com/se/Login/Login.fxml"));
        stage.initStyle(StageStyle.UNDECORATED);

        Scene scene = new Scene(fxmlLoader.load());

        stage.getIcons().add(new Image(Objects.requireNonNull(getClass().getResourceAsStream("/image/Icon.png"))));


        //设置拖动界面
        AtomicReference<Double> offsetX = new AtomicReference<>((double) 0);
        AtomicReference<Double> offsetY = new AtomicReference<>((double) 0);
        scene.setOnMousePressed(event->{
            offsetX.set(event.getSceneX());
            offsetY.set(event.getSceneY());
        });
        scene.setOnMouseDragged(event->{
            stage.setX(event.getScreenX()-offsetX.get());
            stage.setY(event.getScreenY()-offsetY.get());
        });

        //右上角关闭页面按钮实现


        stage.setTitle("登录");
        stage.setScene(scene);
        stage.setResizable(false);
        stage.setOnCloseRequest(event -> {
            ServiceUtils.closeConnection();
            StageManager.remove(StageType.LOGIN_STAGE);
            Platform.exit();
        });

        StageManager.add(StageType.LOGIN_STAGE, stage);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}